package com.x.erp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.erp.dao.ShareCarDao;
import com.x.erp.dao.ShareTemplateDao;
import com.x.erp.manage.ShareTemplateManage;
import com.x.erp.model.Car;
import com.x.erp.model.ShareCar;
import com.x.erp.model.ShareTemplate;
import com.x.erp.rpc.ShareTemplateRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 分享模板服务
 */
@Component
@Service(interfaceName = "com.x.erp.rpc.ShareTemplateRpcService", timeout = 60000)
public class ShareTemplateService extends BaseService implements ShareTemplateRpcService {

	@Autowired
	private ShareTemplateDao shareTemplateDao; // 分享模板Dao

	@Autowired
	private ShareTemplateManage shareTemplateManage; // 分享模板Manage

	@Autowired
	private ShareCarDao shareCarDao; // 分享模板Dao

	/**
	 * 增加分享模板信息
	 *
	 * @param shareTemplate
	 * @return
	 */
	@Override
	public ShareTemplate insertShareTemplate(ShareTemplate shareTemplate) {
		try {
			return shareTemplateManage.insertShareTemplate(shareTemplate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 删除车辆列表分享模板信息
	 *
	 * @param shareTemplate
	 * @return
	 */
	@Override
	public int deleteShareTemplate(ShareTemplate shareTemplate) {
		try {
			return shareTemplateManage.deleteShareTemplate(shareTemplate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 修改车辆列表分享模板信息
	 *
	 * @param shareTemplate
	 * @return
	 */
	@Override
	public int updateShareTemplate(ShareTemplate shareTemplate) {
		try {
			return shareTemplateManage.updateShareTemplate(shareTemplate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询表分享模板信息
	 *
	 * @param shareTemplate
	 * @return
	 */
	@Override
	public ShareTemplate selectShareTemplate(ShareTemplate shareTemplate) {
		try {
			ShareTemplate res = shareTemplateDao.selectShareTemplate(shareTemplate);
			ShareCar tempShareCar = new ShareCar();
			tempShareCar.setShareId(res.getShareId());
			List<ShareCar> shareCarList = shareCarDao.selectShareCarList(tempShareCar);
			Car[] cars = new Car[shareCarList.size()];
			for (int i = 0; i < shareCarList.size(); i++) {
				tempShareCar = shareCarList.get(i);
				Integer seq = tempShareCar.getShareSeq();
				if(seq != null && seq > 0){
					cars[seq-1] = tempShareCar.getCar();
				}
			}
			res.setCars(Arrays.asList(cars));
			return res;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询分享模板信息列表
	 *
	 * @param shareTemplate
	 * @return
	 */
	@Override
	public List<ShareTemplate> selectShareTemplateList(ShareTemplate shareTemplate) {
		try {
			return shareTemplateDao.selectShareTemplateList(shareTemplate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}
}
